package com.example.transit.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.transit.R;
import com.example.transit.adapter.CollectionAdapter;
import com.example.transit.base.BaseFragment;
import com.example.transit.bean.CollectionBean;
import com.example.transit.contentprovider.DbCollectionInfo;
import com.example.transit.event.CollectionEvent;
import com.example.transit.event.SpHistroyEvent;
import com.example.transit.ui.activity.BusRunActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class CollectionFragment extends BaseFragment {
    @BindView(R.id.rv_collection)
    RecyclerView rvCollection;
    Unbinder unbinder;


    private List<CollectionBean> collectionBeanList;
    private CollectionAdapter collectionAdapter;
    DbCollectionInfo dbCollectionInfo;
    //收藏点击
    Boolean isCollSelected = true;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.fragment_collection;
    }

    @Override
    public void initView() {
        collectionBeanList = new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//            CollectionBean bean = new CollectionBean();
//            bean.setName(i + "");
//            collectionBeanList.add(bean);
//        }
        dbCollectionInfo = new DbCollectionInfo(getContext().getContentResolver(), getActivity());
        collectionBeanList=dbCollectionInfo.getAllCollection();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCollection.setLayoutManager(layoutManager);
        collectionAdapter = new CollectionAdapter(R.layout.item_collection, collectionBeanList);
        rvCollection.setAdapter(collectionAdapter);
        rvCollection.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                int itemViewId=view.getId();
                switch (itemViewId) {
                    case R.id.collect_list_img:
                        CollectionBean collectionBean=new CollectionBean();
                        collectionBean.setLineNum(collectionBeanList.get(position).getLineNum());
                        collectionBean.setName(collectionBeanList.get(position).getName());
                        collectionBean.setIsupline(collectionBeanList.get(position).getIsupline());
                        if (isCollSelected){
                            isCollSelected=false;
                            if (dbCollectionInfo.Dup_check(collectionBean)){
                                dbCollectionInfo.deleteCollect(collectionBean);
                            }
                        }else {
                            isCollSelected=true;
                            if (!dbCollectionInfo.Dup_check(collectionBean)){
                                dbCollectionInfo.insertCollectInfo(collectionBean);
                            }
                        }
                        view.setSelected(isCollSelected);

                        break;
                    case R.id.collect_list_lly:
                        CollectionEvent collectionEvent=new CollectionEvent();
                        Log.d("may", "collectionBeanList " + collectionBeanList.get(position).getName()+" "+collectionBeanList.get(position).getIsupline()+" "+collectionBeanList.get(position).getLineNum());
                        collectionEvent.setStaName(collectionBeanList.get(position).getName());
                        collectionEvent.setIsupline(collectionBeanList.get(position).getIsupline());
                        collectionEvent.setLineNum(collectionBeanList.get(position).getLineNum());
                        EventBus.getDefault().postSticky(collectionEvent);
                        Intent intent = new Intent(getActivity(), BusRunActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                        break;
                }
            }
        });

    }

    @Override
    public void initData() {

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
